/[editor]/editor_main/client/gridlines.lua

http://mtasa-resources.googlecode.com/ · Lua · 145 lines · 119 code · 16 blank · 10 comment · 45 complexity · f2acf9289b0a46eaee820ab26ba6be5e MD5 · raw file

  1. local localPlayer = getLocalPlayer()
  2. local MAX_THICKNESS = 1.2
  3. --local c
  4. local drawLine
  5. --local color = 1694433280
  6. local attachedToElement
  7. --Element types to ignore the element matrix of. They do not have rotation
  8. local ignoreMatrix = {
  9. pickup = true
  10. }
  11. function showGridlines ( element )
  12. attachedToElement = element
  13. return true
  14. end
  15. local function renderGridlines()
  16. if not isElement(attachedToElement) then return end
  17. if getElementDimension(attachedToElement) ~= getElementDimension(localPlayer) then return end
  18. local x,y,z = edf.edfGetElementPosition(attachedToElement)
  19. if not x then return end
  20. local minX,minY,minZ,maxX,maxY,maxZ = edf.edfGetElementBoundingBox ( attachedToElement )
  21. if not minX then
  22. local radius = edf.edfGetElementRadius ( attachedToElement )
  23. if radius then
  24. minX,minY,minZ,maxX,maxY,maxZ = -radius,-radius,-radius,radius,radius,radius
  25. end
  26. end
  27. if not minX or not minY or not minZ or not maxX or not maxY or not maxZ then return end
  28. local camX,camY,camZ = getCameraMatrix()
  29. --Work out our line thickness
  30. local thickness = (100/getDistanceBetweenPoints3D(camX,camY,camZ,x,y,z)) * MAX_THICKNESS
  31. --
  32. local elementMatrix = (getElementMatrix(attachedToElement) and not ignoreMatrix[getElementType(attachedToElement)])
  33. and matrix(getElementMatrix(attachedToElement))
  34. if not elementMatrix then
  35. --Make them into absolute coords
  36. minX,minY,minZ = minX + x,minY + y,minZ + z
  37. maxX,maxY,maxZ = maxX + x,maxY + y,maxZ + z
  38. end
  39. --
  40. local face1 = matrix{
  41. {minX,maxY,minZ,1},
  42. {minX,maxY,maxZ,1},
  43. {maxX,maxY,maxZ,1},
  44. {maxX,maxY,minZ,1},
  45. }
  46. local face2 = matrix{
  47. {minX,minY,minZ,1},
  48. {minX,minY,maxZ,1},
  49. {maxX,minY,maxZ,1},
  50. {maxX,minY,minZ,1},
  51. }
  52. if elementMatrix then
  53. face1 = face1*elementMatrix
  54. face2 = face2*elementMatrix
  55. end
  56. local faces = { face1,face2 }
  57. local drawLines,furthestNode,furthestDistance = {},{},0
  58. --Draw rectangular faces
  59. for k,face in ipairs(faces) do
  60. for i,coord3d in ipairs(face) do
  61. if not getScreenFromWorldPosition(coord3d[1],coord3d[2],coord3d[3],10) then return end
  62. local nextIndex = i + 1
  63. if not face[nextIndex] then nextIndex = 1 end
  64. local targetCoord3d = face[nextIndex]
  65. table.insert ( drawLines, { coord3d, targetCoord3d } )
  66. local camDistance = getDistanceBetweenPoints3D(camX,camY,camZ,unpack(coord3d))
  67. if camDistance > furthestDistance then
  68. furthestDistance = camDistance
  69. furthestNode = faces[k][i]
  70. end
  71. end
  72. end
  73. --Connect these faces together with four lines
  74. for i=1,4 do
  75. table.insert ( drawLines, { faces[1][i], faces[2][i] } )
  76. end
  77. --
  78. for i,draw in ipairs(drawLines) do
  79. if ( not vectorCompare ( draw[1], furthestNode ) ) and ( not vectorCompare ( draw[2], furthestNode ) ) then
  80. drawLine (draw[1],draw[2],tocolor(200,0,0,180),thickness)
  81. end
  82. end
  83. end
  84. function drawXYZLines()
  85. if not isElement(attachedToElement) then return end
  86. if getElementDimension(attachedToElement) ~= getElementDimension(localPlayer) then return end
  87. local radius = getElementRadius(attachedToElement)
  88. local x,y,z = getElementPosition(attachedToElement)
  89. local xx,xy,xz = getPositionFromElementAtOffset(attachedToElement,radius,0,0)
  90. local yx,yy,yz = getPositionFromElementAtOffset(attachedToElement,0,radius,0)
  91. local zx,zy,zz = getPositionFromElementAtOffset(attachedToElement,0,0,radius)
  92. drawLine({x,y,z},{xx,xy,xz},tocolor(200,0,0,200),1.7)
  93. drawLine({x,y,z},{yx,yy,yz},tocolor(0,200,0,200),1.7)
  94. drawLine({x,y,z},{zx,zy,zz},tocolor(0,0,200,200),1.7)
  95. end
  96. function doBasicElementRenders()
  97. if not isElement(attachedToElement) then return end
  98. if exports["editor_gui"]:sx_getOptionData("enableBox") then renderGridlines() end
  99. if exports["editor_gui"]:sx_getOptionData("enableXYZlines") then drawXYZLines() end
  100. end
  101. addEventHandler ( "onClientRender", getRootElement(), doBasicElementRenders )
  102. function getPositionFromElementAtOffset(element,x,y,z)
  103. if not x or not y or not z then
  104. return false
  105. end
  106. local ox,oy,oz = getElementPosition(element)
  107. local matrix = getElementMatrix ( element )
  108. if not matrix then return ox+x,oy+y,oz+z end
  109. local offX = x * matrix[1][1] + y * matrix[2][1] + z * matrix[3][1] + matrix[4][1]
  110. local offY = x * matrix[1][2] + y * matrix[2][2] + z * matrix[3][2] + matrix[4][2]
  111. local offZ = x * matrix[1][3] + y * matrix[2][3] + z * matrix[3][3] + matrix[4][3]
  112. return offX, offY, offZ
  113. end
  114. function drawLine(vecOrigin, vecTarget,color,thickness)
  115. local startX,startY = getScreenFromWorldPosition(vecOrigin[1],vecOrigin[2],vecOrigin[3],10)
  116. local endX,endY = getScreenFromWorldPosition(vecTarget[1],vecTarget[2],vecTarget[3],10)
  117. if not startX or not startY or not endX or not endY then
  118. return false
  119. end
  120. return dxDrawLine ( startX,startY,endX,endY,color,thickness, false)
  121. end
  122. function vectorCompare ( vec1,vec2 )
  123. if vec1[1] == vec2[1] and vec1[2] == vec2[2] and vec1[3] == vec2[3] then return true end
  124. end
  125. function getOffsetRelativeToElement ( element, x, y, z )
  126. local elementMatrix = matrix{getElementMatrix(element)}
  127. elementMatrix = matrix{x,y,z} * elementMatrix
  128. return elementMatrix
  129. end